﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Request.QueryString["login"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome!');", true);
            //}
            //else if (Request.QueryString["contacted"] != null)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Thanks for your feedback!');", true);
            //}


            if (Session["flag"] != null)
            {
                switch (Session["flag"].ToString())
                {
                    case "redirecterror":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showWarningToast', 'Opps! You need to sign in before proceeding!');", true);
                        break;

                    case "login":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome!');", true);
                        break;

                    case "contacted":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Thanks for your feedback!');", true);
                        break;
                    
                    case "notauthorizedgroup":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! You are not authorized to update this group!');", true);
                        break;

                    case "notauthorizedevent":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! You are not authorized to update this event!');", true);
                        break;

                    case "notfound":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! The resource you are looking for does not exists!');", true);
                        break;

                    case "firsttime":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Thanks for registering with us!');", true);
                        break;
                }

                Session["flag"] = null;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "", true);
            }


            //if (!IsPostBack)
            //{
            if (Session["Member"] == null)
            {
                toptest.Visible = true;
                DivSignout.Visible = false;
                topUsers.Visible = false;
                // tbxHeaderEmail1.Focus();
            }
            else
            {
                toptest.Visible = false;
                DivSignout.Visible = true;
                topUsers.Visible = true;

                int unreadMsg = NotificationControl.NoOfUnreadMessage(Session["Member"].ToString());

                if (unreadMsg > 0)
                {
                    string msg = "$().toastmessage('showNoticeToast', 'Hey! You have {0} unread notification{1}!');";

                    if (unreadMsg == 1)
                    {
                        msg = String.Format(msg, unreadMsg, "");
                    }
                    else
                    {
                        msg = String.Format(msg, unreadMsg, "s");
                    }

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", msg, true);
                }
            }
            //}
        }

        protected void btnHeaderSignIn_Click1(object sender, EventArgs e)
        {
            Member m = MemberControls.VerifyAccount(tbxHeaderEmail1.Text, tbxHeaderPassword1.Text);
            if (m!= null)
            {
                Session["Member"] = m.MembershipNo;
                //Response.Redirect("~/index.aspx?login=" + Session["Member"].ToString());

                Session["flag"] = "login";
                Response.Redirect("~/index.aspx");
            }
            else
            {
                //SHOW login faliure....

                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps, Wrong login information was provided!');", true);
                 

            }
        }

        protected void btnSignout_Click(object sender, EventArgs e)
        {
            Session["Member"] = null;

            Session.Abandon();

            Response.Redirect("~/index.aspx");
        }

        protected void btnHeaderSignUp_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/registration.aspx");
        }

        protected void btnRmbMe_Click(object sender, EventArgs e)
        {
            if (tbxHeaderEmail1.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! Email textfield was left empty!');", true);
                return;
            }


            //if account exist...
            bool isAccountExists = MemberControls.IsAccountExist(tbxHeaderEmail1.Text);

            if (isAccountExists)
            {
                Utility.EmailNewPasswordToUser(Request, tbxHeaderEmail1.Text);

                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Check your email for your new password!');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! Wrong email was provided!');", true);
            }

        } 
    }
}
